package cc.facesign.admin.service.impl;

import cc.facesign.admin.bean.vo.UserVo;
import cc.facesign.admin.bean.xls.AddConfUserXls;
import cc.facesign.admin.bean.xls.AddDeptUserXls;
import cc.facesign.admin.bean.xls.ExportConfUserXls;
import cc.facesign.admin.client.OssClient;
import cc.facesign.admin.dao.xls.XlsDao;
import cc.facesign.admin.enums.GenderType;
import cc.facesign.admin.enums.SignType;
import cc.facesign.admin.listener.ConfUserXlsListener;
import cc.facesign.admin.listener.DeptUserXlsListener;
import cc.facesign.admin.service.TblConfService;
import cc.facesign.admin.service.XlsService;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class XlsServiceImpl implements XlsService {

    @Autowired
    private XlsDao xlsDao;

    @Autowired
    private TblConfService confService;

    @Autowired
    private OssClient ossClient;

    /**
     * 向部门中添加用户
     * @param deptId
     * @param file
     * @param pics
     * @return
     */
    public List<AddDeptUserXls> addUserForDept(String deptId, MultipartFile file, List<MultipartFile> pics) {
        InputStream in = null;
        try {
            in = file.getInputStream();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        DeptUserXlsListener listen = new DeptUserXlsListener(xlsDao, pics, deptId);
        EasyExcel.read(in, AddDeptUserXls.class, listen).sheet().doRead();

        return listen.getErrs();
    }

    /**
     * 向会议中添加用户
     * @param file
     * @param confId
     * @return
     */
    public List<AddConfUserXls> addUserForConf(MultipartFile file, String confId) {

        InputStream in = null;
        try {
            in = file.getInputStream();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        ConfUserXlsListener listen = new ConfUserXlsListener(xlsDao, confId);
        EasyExcel.read(in, AddConfUserXls.class, listen).sheet().doRead();

        return listen.getErrs();
    }

    @Override
    public String exportUserForConf(HttpServletResponse response,  List<UserVo> records) throws IOException {

        // 头的策略
        WriteCellStyle headWriteCellStyle = new WriteCellStyle();
        // 背景设置为黄色
        headWriteCellStyle.setFillForegroundColor(IndexedColors.YELLOW.getIndex());
        WriteFont headWriteFont = new WriteFont();
        headWriteFont.setFontHeightInPoints((short)12);
        headWriteCellStyle.setWriteFont(headWriteFont);
        // 内容的策略
        WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
        contentWriteCellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND);
        contentWriteCellStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex());
        WriteFont contentWriteFont = new WriteFont();
        contentWriteFont.setFontHeightInPoints((short)12);
        contentWriteCellStyle.setWriteFont(contentWriteFont);
        HorizontalCellStyleStrategy horizontalCellStyleStrategy =
                new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);


        // 这里 需要指定写用哪个class去写，然后写到第一个sheet，名字为模板 然后文件流会自动关闭
        ServletOutputStream out = response.getOutputStream();
        EasyExcel.write(out, ExportConfUserXls.class)
                .registerWriteHandler(horizontalCellStyleStrategy)
                .sheet("用户信息")
                .doWrite(exportUserForConfData(records));

        return null;
    }

    private List<ExportConfUserXls> exportUserForConfData(List<UserVo> list) {
        List<ExportConfUserXls> res = new ArrayList<>();

        for (UserVo vo : list) {
            ExportConfUserXls xls = new ExportConfUserXls();
            BeanUtils.copyProperties(vo, xls);

            String s = GenderType.codeOf(vo.getGender());
            xls.setGender(s);

            xls.setIsSign(SignType.codeOf(vo.getIsSign()));
            res.add(xls);
        }

        return res;
    }

}
